//
//  LSYhotCommentsView.m
//  Cartoon
//
//  Created by dllo on 15/7/16.
//  Copyright (c) 2015年 dllo. All rights reserved.
//
#import "BaseHeader.h"
#import "LSYhotCommentsView.h"
#import "LSYFastLookHotTableViewCell.h"
#import "AFNetworkHandler.h"

@interface LSYhotCommentsView ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *HottableView;
@property (nonatomic, retain) NSMutableArray *hotCommentsDataArr;
@end
@implementation LSYhotCommentsView
- (void)dealloc
{
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame hotURL:(NSString *)hotURL
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        [self getData:hotURL];
    }
    return self;
}

- (void)createTableView
{
    self.HottableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    self.HottableView.delegate = self;
    self.HottableView.dataSource = self;
    self.HottableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 15);
    self.HottableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self addSubview:self.HottableView];
    [_HottableView release];
}

#pragma mark - tableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.hotCommentsDataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSYFastLookHotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"l"];
    if (!cell) {
        cell = [[[LSYFastLookHotTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"l"] autorelease];
    }
    //取消选中效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.hotCommentsDataArr[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSYFastLookHotCommentsModel *model = self.hotCommentsDataArr[indexPath.row];
    CGFloat h = [LSYFastLookHotTableViewCell heightWithText:model.content];
    return h + 80 * SCREEN_HEIGHT / 667;
}

+ (instancetype)hotcommentsWithinitFrame:(CGRect)frame hotURL:(NSString *)hotURL
{
    id result = [[[self alloc] initWithFrame:frame hotURL:hotURL] autorelease];
    return result;
}

- (void)getData:(NSString *)hotURL
{
    NSString *strTemp = [@"http://api.kuaikanmanhua.com/v1/comics/" stringByAppendingString:hotURL];
    NSString *str = [strTemp stringByAppendingString:@"/comments?order=score&offset=0&limit=20"];
    dispatch_queue_t queue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue2, ^{
        [AFNetworkHandler GETwithAFNByURL:str completion:^(id result) {
            NSDictionary *tempDic = [result objectForKey:@"data"];
            NSArray *tempArr = [tempDic objectForKey:@"comments"];
            self.hotCommentsDataArr  = [LSYFastLookHotCommentsModel arrayWithModelByArray:tempArr];
            [self createTableView];
        }];
    });

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
